package com.smart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.common.PageInfo;
import com.smart.model.dtomapper.DictDtoMapper;
import com.smart.model.domain.DictDO;
import com.smart.mapper.DictMapper;
import com.smart.model.request.DictRequest;
import com.smart.service.DictService;
import com.smart.model.convert.DictConvert;
import com.smart.model.vo.DictVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, DictDO> implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Override
    public DictVO getByKey(long id) {
        DictDO dictDO = getById(id);
        DictVO dictVO = DictDtoMapper.INSTANCE.do2VO(dictDO);
        return dictVO;
    }

    @Override
    public PageInfo<DictVO> pageQuery(DictRequest request) {
        long count = dictMapper.pageCount(request);
        List<DictDO> list = dictMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<DictVO> dictVOList = DictConvert.buildDictVoList(list);
        return new PageInfo(request.getPageNum(), request.getPageSize(), dictVOList, count);
    }

    @Override
    public List<DictVO> listQuery(DictRequest request) {
        List<DictDO> list = list();
        List<DictVO> dictVOS = DictConvert.buildDictVoList(list);
        return dictVOS;
    }

    @Override
    public Boolean add(DictRequest request) {
        DictDO dictDO = DictDtoMapper.INSTANCE.request2DO(request);
        save(dictDO);
        return true;
    }

    @Override
    public Boolean updateByKey(DictRequest request) {
        DictDO dictDO = DictDtoMapper.INSTANCE.request2DO(request);
        save(dictDO);
        return true;
    }
}